package splug.cjk;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;
import java.net.UnknownHostException;

import splug.cjk.packet.AssignPacket;

import android.app.*;
import android.os.*;
import android.view.View;
import android.widget.*;

public class Assign extends Activity{

	private String id;
	private String pw;
	private String name;
	private String car;
	private String phone;
	private String mail;

	private Button bt_ok;
	private Button bt_cancel;

	private EditText ed_id;
	private EditText ed_pw;
	private EditText ed_name;
	private EditText ed_car;
	private EditText ed_phone;
	private EditText ed_mail;


	public void onCreate(Bundle savedInstanceState) {

		super.onCreate(savedInstanceState);
		setContentView(R.layout.assign);

		//가입 정보 방아오기
		ed_id = (EditText) findViewById(R.id.EditText04);
		ed_pw = (EditText) findViewById(R.id.editText2);
		ed_name = (EditText) findViewById(R.id.EditText02);
		ed_car = (EditText) findViewById(R.id.EditText01);
		ed_phone = (EditText) findViewById(R.id.editText1);
		ed_mail = (EditText) findViewById(R.id.EditText03);        

		//버튼 정보 받아오기
		bt_ok = (Button) findViewById(R.id.button1);
		bt_cancel = (Button) findViewById(R.id.button2);

		//리스너 등록
		bt_ok.setOnClickListener(ok);
		bt_cancel.setOnClickListener(cancel);



	}

	private View.OnClickListener ok = new View.OnClickListener() {

		@Override
		public void onClick(View v) {

			boolean checkerr = false;
			while(true){
				id = ed_id.getText().toString().toLowerCase().trim(); //id 받기
				if(id.length() <5){
					Toast.makeText(Assign.this,"id는 5자 이상이여야 합니다.", Toast.LENGTH_SHORT).show();
					break;
				}
				
				if(id.indexOf("운영자") != -1){
					Toast.makeText(Assign.this,"금지된 아이디 입니다", Toast.LENGTH_SHORT).show();
					break;
				}
				for(int i = 0 ; i < id.length() ; i++){
					char check = id.charAt(i);
					if( !(((int)check >= 48&&(int)check <=57) || ((int)check >= 97&&(int)check <=122)) ){
						
						ed_id.setText("");
						checkerr = true;
						break;
					}
				}
				if(checkerr){
					Toast.makeText(Assign.this,"id는 숫자와 영어로만 만들수 있습니다!", Toast.LENGTH_SHORT).show();
					break;
				}
				pw = ed_pw.getText().toString();
				if(pw.length()<5){
					Toast.makeText(Assign.this,"비밀번호는 5자 이상이여야 합니다.", Toast.LENGTH_SHORT).show();
					break;
				}
				for(int i = 0 ; i < pw.length() ; i++){
					char check = pw.charAt(i);
					if( !(check<=126&&check>=33) ){
						ed_pw.setText("");
						checkerr = true;
						break;
					}
				}		
				if(checkerr){
					Toast.makeText(Assign.this,"비밀번호는 영어,숫자,특수문자의 조합으로 만들어주세요!", Toast.LENGTH_SHORT).show();
					break;
				}

				name = ed_name.getText().toString();
				if(name.length() == 0){
					Toast.makeText(Assign.this,"이름을 입력해주세요", Toast.LENGTH_SHORT).show();
					break;
				}
				if(name.indexOf("운영자") != -1){
					Toast.makeText(Assign.this,"금지된 이름 입니다", Toast.LENGTH_SHORT).show();
					break;
				}
				car = ed_car.getText().toString();
				if(car.length() == 0){
					Toast.makeText(Assign.this,"자동차 번호를 입력해주세요!", Toast.LENGTH_SHORT).show();
					break;
				}
				
				phone = ed_phone.getText().toString();
				if(phone.length() == 0){
					Toast.makeText(Assign.this,"전화번호를 입력해주세요!", Toast.LENGTH_SHORT).show();
					break;
				}
				for(int i = 0 ; i < phone.length() ; i++){
					char check = phone.charAt(i);
					if( !(((int)check >= 48&&(int)check <=57)) ){
						ed_phone.setText("");
						checkerr = true;
						break;
					}
				}		
				if(checkerr){
					Toast.makeText(Assign.this,"전화번호는 - 제외 숫자로만 입력해주세요!", Toast.LENGTH_SHORT).show();
					break;
				}

				mail = ed_mail.getText().toString();
				if(mail.length() == 0){
					Toast.makeText(Assign.this,"메일을 입력해주세요!", Toast.LENGTH_SHORT).show();
					break;
				}
				checkerr = true;
				for(int i = 0 ; i < mail.length() ; i++){
					char check = mail.charAt(i);					
					if(check == '@' ){
						checkerr = false;
						break;
					}

				}		
				if(checkerr){
					ed_mail.setText("");
					Toast.makeText(Assign.this,"올바른 email형식이 아닙니다", Toast.LENGTH_SHORT).show();
					break;
				}

				try {

					Socket socket = new Socket(LetsGoActivity.server_ip, 2007);
					AssignPacket packet = new AssignPacket(id,pw,name,car,phone,mail,0);
					
					InputStream is = socket.getInputStream();
					OutputStream os = socket.getOutputStream();
					
					OutputStreamWriter osw = new OutputStreamWriter(os);
					InputStreamReader isr = new InputStreamReader(is);
					
					osw.write('0');
					osw.flush();
					
					int ack = isr.read();
					
					if(ack == '1'){
						ObjectOutputStream output = new ObjectOutputStream(os);
						output.writeObject(packet);
						
						ack = isr.read();
						
						if(ack == '1'){
							Toast.makeText(Assign.this,"회원 가입이 완료되었습니다 !", Toast.LENGTH_SHORT).show();
							finish();
						}
						else{
							Toast.makeText(Assign.this,"동일한 ID가 있습니다 !", Toast.LENGTH_SHORT).show();
							ed_id.setText("");
							break;
						}
					}
					else{
						Toast.makeText(Assign.this, "네트워크 에러", Toast.LENGTH_SHORT).show();
						break;
					}					

				} catch (UnknownHostException e) {
					e.printStackTrace();
				} catch (IOException e) {
					e.printStackTrace();
				}
				
				break;
			}




		}
	};


	private View.OnClickListener cancel = new View.OnClickListener() {

		@Override
		public void onClick(View v) {
			finish();
		}
	};


}
